#!/usr/bin/perl

###########################################################################
## A script to convert a Palm/Visor Strip encryped database to
## SpashID CSV format.
##
## Background: Strip is free password manager from zetetic for Palm/Visor.
## I use Strip 1.0 which uses uses AES for encryption.
##
## The only reason I was using Visor is for Strip. Lately with 
## iPhone firmware 2.0, I noticed a application called SplashID,
## which seems  very nice and I think I can get rid of my Visor finally.
##
## I've nothing to do with SplashID, I just use their software.
##
## Copyright: GNU General Public License v2
## 
## http://strip2csv.googlecode.com/
## muquit@muquit.com Sep-05-2009 
## 
###########################################################################

use strict;
use English;
use FileHandle;
use Fcntl;
use File::Basename;
use Getopt::Long;
use POSIX qw(tmpnam);
#use Term::ReadKey;
use Palm::Zetetic::Strip;

$|=1;

#--- globals -- starts
my $version_s="1.01";
#--- globals -- ends

&doit();
# won't be here

sub doit
{
    my $argc=$#ARGV+1;
    if ($argc != 4)
    {
        print <<EOF;
  strip2csv v$version_s
  http://strip2csv.googlecode.com/

  usage: $0 <SplashID Type> \\
            <SplasdID Category> \\
            <directory of Strip databases> \\
            <csv file>
EOF
;
        exit(1);
    }

    my $splashid_type=$ARGV[0];
    my $splashid_category=$ARGV[1];
    my $strip_dir=$ARGV[2];
    if (! -d $strip_dir)
    {
        &print_fatal("Error: Directory $strip_dir does not exist");
    }
    my $csv_file=$ARGV[3];


#    my $strip_pass=&my_getpass("Enter Strip password: ");
    my $strip_pass=$ENV{"STRIP_PASSWD"};
    if (!$strip_pass)
    {
        print STDERR <<EOF;

 Error: No Strip password specified.
 Set strip database password to environment variable STRIP_PASSWD");
 Example:
 STRIP_PASSWD="secret"
 export STRIP_PASSWD
EOF
;
 exit(1);
    }

    &print_splashid_csv($splashid_type,$splashid_category,$strip_dir,$strip_pass,$csv_file);
}

sub put_quotes_around()
{
    my $str=shift;
    if (!$str)
    {
        return($str);
    }
    $str="\"$str\"";
    return($str);
}

sub print_splashid_csv()
{
    my $splashid_type=shift;
    my $splashid_category=shift;
    my $strip_dir=shift;
    my $strip_pass=shift;
    my $csv_file=shift;
    local *FD;

    my @months=("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

    my @ta=localtime(time);
    my $year=1900 + $ta[5];
    my $dd=$ta[3];
    my $len=length($dd);
    if ($len == 1)
    {
        $dd="0${dd}";
    }

    my $today="\"$months[$ta[4]] $dd, $year\"";

    my $strip=new Palm::Zetetic::Strip();
    $strip->set_directory("$strip_dir");
    print "\n";
    if (! $strip->set_password("$strip_pass"))
    {
        &print_fatal("Error: Incorrent password");
    }

    $strip->load();
    my @systems=$strip->get_systems();
    my $system;
    my @accounts;
    my $account;

#    $splashid_type=&put_quotes_around($splashid_type);

    open(FD,">$csv_file") or die "Could not open csv file '$csv_file' $!";

#Strip,test.com,test,secret,http://test.com/,f5,f6,f7,f8,f9,"July 27,2008",note1 l1note2l2,Personal
    my $e='';
    foreach $system (@systems)
    {
        my ($category,$system_name,$username,$password,$comment);
        my (@password,@comment,$opening_comment);
        @accounts = $strip->get_accounts($system);
        next if (@accounts == 0);
        $category = $system->get_name();
        $category=&put_quotes_around($category);

        foreach $account (@accounts)
        {
            my @pa=();
            my @ca=();
            $system_name=$account->get_system();
            $username=$account->get_username();
            $password = $account->get_password();
            $comment = $account->get_comment();

            if ($password)
            {
                @pa=split('\n',$password);
            }
            if ($comment)
            {
                @ca=split('\n',$comment);
            }

            my $new_pass='';
            my $p;

            if (scalar(@pa))
            {
                foreach $p (@pa)
                {
                    $new_pass .= "$p ";
                }
                $new_pass =~ s/\s+$//g;
            }
            if ($new_pass)
            {
                $new_pass=&put_quotes_around($new_pass);
            }

            $system_name=&put_quotes_around($system_name);
            if ($username)
            {
                $username=&put_quotes_around($username);
            }
            if ($new_pass)
            {
            }

        print FD "$splashid_type,$category,$username,$new_pass,$system_name,$e,$e,$e,$e,$e,$today,";
            my $ftime=1;
            if (scalar(@ca))
            {
                foreach (@ca)
                {
                    if ($ftime)
                    {
                        print FD "\"";
                        $ftime=0;
                    }
                    print FD "$_\013";
                }
                if ($ftime == 0)
                {
                    print FD "\"";
                }
            }
            print FD ",$splashid_category\n";
            $ftime=0;
        }
    }
    close(FD);
}

##---
# returns "unix" if it is not mswin32
##---
sub os_type()
{
    my $os=$^O;
    if ($os !~ /mswin32/i)
    {
        $os="unix";
    }
    return($os);
}


##-----
# reset tty
##-----
sub reset_tty()
{
    if (&os_type() !~ /mswin32/i)
    {
        system("stty sane");
        system("stty echo");
        system("stty erase ^H");
    }
}

##-----
# print the message to stderr and exit
##-----
sub print_fatal()
{
    my $msg=shift;
    print "$msg\n";
    exit(1);
}
